
class com.mochimedia.ui.DrawingMethods {

    /**
     * Method: roundedRect
     * Draws a rounded rectangle inside the movieclip
     * 
     * @param   mc the movieclip in which to draw
     * @param   clear erase any drawing in the mc
     * @param   x the x coordinate
     * @param   y the y coordinate
     * @param   boxWidth width of rectangle
     * @param   boxHeight height of rectangle
     * @param   cornerRadius corner radius of one corner or CSS-style space-separated string of 4 corners T R B L (optional)
     * @param   colors array of colors to use in the fill (optional)
     * @param   alphas array of alphas to use in the fill (optional)
     * @param   percentages array of percentages to use in the fill (optional)
     * @param   matrix a matrix object to use for the gradient (optional)
     * @param   lineThickness line thickness (optional)
     * @param   lineColor line color (optional)
     * @param   lineAlpha transparency of line (optional)
     */
    
     
    public static function roundedRect (mc:MovieClip, clear:Boolean, x:Number, y:Number, boxWidth:Number, boxHeight:Number, cornerRadius:String, colors:Array, alphas:Array, percentages:Array, matrix:Object, lineThickness:Number, lineColor:Number, lineAlpha:Number) {
            
        var cornerRadii:Array = new Array();

        var cornerT:Number = 0;
        var cornerR:Number = 0;
        var cornerB:Number = 0;
        var cornerL:Number = 0;
        
        if (clear != false) mc.clear();
        
        // validate values
        
        var i:Number;
        
        if (colors.length < 1) colors = [0x000000];
        
        if (alphas.length != colors.length) {
            alphas = [];
            for (i = 0; i < colors.length; i++) {
                alphas.push(100);
            }
        }
        
        if (percentages.length != colors.length) {
            percentages = [];
            for (i = 0; i < colors.length; i++) {
                percentages.push((i / (colors.length - 1)) * 255);
            }           
        }

        if (cornerRadius == undefined || cornerRadius == "") cornerRadius = "0";

        cornerRadii = cornerRadius.split(" ");
        cornerT = parseInt(cornerRadii[0]);
        cornerR = (cornerRadii[1] == undefined) ? cornerT : parseInt(cornerRadii[1]);
        cornerB = (cornerRadii[2] == undefined) ? cornerT : parseInt(cornerRadii[2]);
        cornerL = (cornerRadii[3] == undefined) ? cornerR : parseInt(cornerRadii[3]);

        x = (!isNaN(x)) ? x : 0;
        y = (!isNaN(y)) ? y : 0;
        if (isNaN(boxWidth)) boxWidth = mc._width;
        if (isNaN(boxHeight)) boxHeight = mc._height;
        
        if (!isNaN(lineThickness) && lineThickness > 0) {
            if (lineColor == undefined || !isNaN(lineColor) || lineColor == 0) lineColor = 0x000000;
            if (lineAlpha == undefined || !isNaN(lineAlpha) || lineAlpha == 0) lineAlpha = 100;
            mc.lineStyle(lineThickness, lineColor, lineAlpha);
        }

        if (colors.length == 1) {
            
            // solid fill
            //
            mc.beginFill(colors[0], alphas[0]);
            
        } else {
            
            // gradient fill
            //
            if (matrix == null) matrix = { matrixType:"box", x: x, y: y, w: boxWidth, h: boxHeight, r: 90 * (Math.PI / 180) };
            mc.beginGradientFill("linear", colors, alphas, percentages, matrix);
            
        }

        //
        if (cornerT > 0) {
            mc.moveTo(x + cornerT, y);
        } else {
            mc.moveTo(x, y);
        }
        
        if (cornerR > 0) {
            mc.lineTo(x + boxWidth-cornerR, y);
            mc.curveTo(x + boxWidth, y, x + boxWidth, y + cornerR);
            mc.lineTo(x + boxWidth, y + cornerR);
        } else {
            mc.lineTo(x + boxWidth, y);
        }
        
        if (cornerB > 0) {
            mc.lineTo(x + boxWidth, y + boxHeight-cornerB);
            mc.curveTo(x + boxWidth, y + boxHeight, x + boxWidth - cornerB, y + boxHeight);
            mc.lineTo(x + boxWidth - cornerB, y + boxHeight);
        } else {
            mc.lineTo(x + boxWidth, y + boxHeight);
        }
        
        if (cornerL > 0) {
            mc.lineTo(x + cornerL, y + boxHeight);
            mc.curveTo(x, y + boxHeight, x, y + boxHeight - cornerL);
            mc.lineTo(x, y + boxHeight-cornerL);
        } else {
            mc.lineTo(x, y + boxHeight);
        }
        
        if (cornerT > 0) {
            mc.lineTo(x, y + cornerT);
            mc.curveTo(x, y, x + cornerT, y);
            mc.lineTo(x + cornerT, y);
        } else {
            mc.lineTo(x, y);
        }
        
        mc.endFill();
        
    }
    
    /**
     * Method: emptyRect
     * Draws a rectangle with a hole in the middle
     * 
     * @param   mc the movieclip in which to draw
     * @param   clear erase any drawing in the mc
     * @param   x the x coordinate
     * @param   y the y coordinate
     * @param   boxWidth width of rectangle
     * @param   boxHeight height of rectangle
     * @param   thickness thickness of rectangle edges
     * @param   fillColor the color of the rect
     */
    
     
    public static function emptyRect (mc:MovieClip, clear:Boolean, x:Number, y:Number, boxWidth:Number, boxHeight:Number, thickness:Number, fillColor:Number, fillAlpha:Number) {
            
        if (clear != false) mc.clear();
        
        // validate values

        x = (!isNaN(x)) ? x : 0;
        y = (!isNaN(y)) ? y : 0;
        if (isNaN(boxWidth)) boxWidth = mc._width;
        if (isNaN(boxHeight)) boxHeight = mc._height;

        if (isNaN(fillColor)) fillColor = 0x000000;
        if (isNaN(fillAlpha)) fillAlpha = 100;
        if (isNaN(thickness)) thickness = 1;

        mc.beginFill(fillColor, fillAlpha);

        mc.moveTo(x, y);
        mc.lineTo(x + boxWidth, y);
        mc.lineTo(x + boxWidth, y + boxHeight);
        mc.lineTo(x, y + boxHeight);
        mc.lineTo(x, y);
        
        mc.lineTo(x + thickness, y + thickness);
        mc.lineTo(x + boxWidth - thickness, y + thickness);
        mc.lineTo(x + boxWidth - thickness, y + boxHeight - thickness);
        mc.lineTo(x + thickness, y + boxHeight - thickness);
        mc.lineTo(x + thickness, y + thickness);
        
        mc.endFill();
        
    }
    
    //
    //
    public static function rect (mc:MovieClip, clear:Boolean, x:Number, y:Number, boxWidth:Number, boxHeight:Number, fillColor:Number, fillAlpha:Number) {
            
        if (clear != false) mc.clear();
        
        x = (!isNaN(x)) ? x : 0;
        y = (!isNaN(y)) ? y : 0;
        if (isNaN(boxWidth)) boxWidth = mc._width;
        if (isNaN(boxHeight)) boxHeight = mc._height;

        if (isNaN(fillColor)) fillColor = 0x000000;
        if (isNaN(fillAlpha)) fillAlpha = 100;

        mc.beginFill(fillColor, fillAlpha);

        mc.moveTo(x, y);
        mc.lineTo(x + boxWidth, y);
        mc.lineTo(x + boxWidth, y + boxHeight);
        mc.lineTo(x, y + boxHeight);
        mc.lineTo(x, y);
        
        mc.endFill();
        
    }
    
    //
    //
    public static function circle (mc:MovieClip, clear:Boolean, x:Number, y:Number, radius:Number, accuracy:Number, fillColor:Number, fillAlpha:Number, lineThickness:Number, lineColor:Number, lineAlpha:Number):Void {
        
        if (clear != false) mc.clear();
        if (radius == 0 || isNaN(radius)) return;
        
        if (isNaN(fillColor)) fillColor = 0x000000;
        if (isNaN(fillAlpha)) fillAlpha = 100;
        if (isNaN(lineColor)) lineColor = 0x000000;
        if (isNaN(lineThickness)) lineThickness = 0;
        
        if (lineThickness > 0) {
            mc.lineStyle(lineThickness, lineColor, 100);
        } else {
            mc.lineStyle();
        }

        mc.beginFill(fillColor, fillAlpha);
        
        if (accuracy < 8) accuracy = 8;
        var span:Number = Math.PI / accuracy;
        var controlRadius:Number = radius / Math.cos(span);
        var anchorAngle:Number = 0;
        var controlAngle:Number = 0;
        
        mc.moveTo(x + Math.cos(anchorAngle) * radius, y + Math.sin(anchorAngle) * radius);
        
        for (var i = 0; i < accuracy; ++i) {
            
            controlAngle = anchorAngle+span;
            anchorAngle = controlAngle+span;
            mc.curveTo(   x + Math.cos(controlAngle)*controlRadius,
                        y + Math.sin(controlAngle)*controlRadius,
                    x + Math.cos(anchorAngle)*radius,
                y + Math.sin(anchorAngle)*radius
            );
            
        }
        
        mc.endFill();
        
    }

}
